package com.ziyun.erp.modules.remittance.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ziyun.erp.modules.remittance.dao.RemittanceDao;
import com.ziyun.erp.modules.remittance.entity.BankInfoEntity;
import com.ziyun.erp.modules.remittance.entity.RemittanceEntity;
import com.ziyun.erp.modules.remittance.entity.RemittanceEntityVo;
import com.ziyun.erp.modules.remittance.service.RemittanceService;
import com.ziyun.erp.modules.supp.entity.SellerInfoEntity;
import com.ziyun.erp.modules.supp.service.SellerInfoService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("remittanceService")
public class RemittanceServiceImpl extends ServiceImpl<RemittanceDao, RemittanceEntity> implements RemittanceService {
    @Autowired
    private SellerInfoService sellerInfoService;

    @Override
    public PageUtils list(Map<String, Object> params) {
        Page<RemittanceEntityVo> page = new Page<>();
        page.setCurrent(Integer.valueOf(params.get("page").toString()));
        page.setSize(Integer.valueOf(params.get("limit").toString()));
        List<RemittanceEntityVo> list = this.baseMapper.list(page, params);
        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    public List<Map<String,Object>> getsellerNames() {
        return this.baseMapper.getsellerNames();
    }

    @Override
    public R insertEntity(RemittanceEntity remittance) {
        //查询该门店是否已经有记录
        List<RemittanceEntity> entityList = this.baseMapper.selectList(new EntityWrapper<RemittanceEntity>().eq("seller_id", remittance.getSellerId()));
        if (entityList.size() > 0) {
            return R.error("该门店已配置打款信息!");
        }
        SellerInfoEntity sellerInfoEntity = sellerInfoService.selectById(remittance.getSellerId());
        remittance.setMerchantType(Integer.valueOf(sellerInfoEntity.getSellerType()));
        if (StringUtils.isNotBlank(remittance.getReceiverAccountNumber())) {
            remittance.setReceiverAccountNumber(remittance.getReceiverAccountNumber().replaceAll(" ",""));
        }
        remittance.setCurrency("人民币");
        remittance.setPaymentType("快速");
        remittance.setBusinessType("普通汇兑");
        remittance.setExpectDay("20080808");
        remittance.setExpectTime("0800");
        remittance.setPaymentBranch("武汉雄楚金地城支行");
        this.baseMapper.insert(remittance);
        return R.ok();
    }

    @Override
    public List<BankInfoEntity> bankInfos() {
        return this.baseMapper.bankInfos();
    }

}
